package com.platform.modules.sys.controller;

import java.util.HashMap;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.platform.common.page.PageUtil;
import com.platform.entity.SysMessageRecEntity;
import com.platform.modules.quality.vo.CommonResult;
import com.platform.modules.sys.dto.MessageListAppDto;
import com.platform.service.SysMessageRecService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 消息管理
 *
 * @author 表华明
 */
@Api(tags = "SysMessageController|消息管理")
@RestController
@RequestMapping("/sys/message")
public class SysMessageController {

    @Autowired
    private SysMessageRecService sysMessageRecService;
    
    /**
     * @descr 查询 消息列表<br>
     * @param currPage
     * @param limit
     * @return
     */
    @ApiOperation(value = "查询 消息列表")
    @PostMapping("/app/list")
    public CommonResult listByAPP(@Valid @RequestBody MessageListAppDto messageListAppDto) {
    	Map<String, Object> params = new HashMap<>();
        params.put("page", PageUtil.getCurrPage(messageListAppDto.getCurrPage()));
        params.put("limit", PageUtil.getLimit(messageListAppDto.getLimit()));
        params.put("userId", messageListAppDto.getUserId());
        params.put("readFlag", messageListAppDto.getReadFlag());
    	Page<SysMessageRecEntity> page = sysMessageRecService.queryPageByApp(params);
    	return CommonResult.success(page);
    } 
    
    /**
     * @descr 消息详情(修改已读标志)<br>
     * @param currPage
     * @param limit
     * @return
     */
    @ApiOperation(value = "消息详情(修改已读标志)")
    @PostMapping("/app/info/{id}")
    public CommonResult info(@PathVariable(name="id") String id) {
    	SysMessageRecEntity sysMessageRecEntity = sysMessageRecService.getById(id);
    	if(null==sysMessageRecEntity) {
    		return CommonResult.error("未找到消息记录");
    	}
    	sysMessageRecEntity.setReadFlag(1);
    	sysMessageRecService.updateById(sysMessageRecEntity);
    		
        return CommonResult.success(sysMessageRecEntity);
    } 
}
